//
//  ViewController.m
//  HoldingBarrage
//
//  Created by lingji001 on 2018/8/21.
//  Copyright © 2018年 lingji001. All rights reserved.
//

#import "ViewController.h"
#import "BottomTextFieldView.h"
#import "BottomCollectionView.h"
#import "HoldingModel.h"
#import "RollingView.h"

@interface ViewController ()

/**
 底部输入框视图
 */
@property (nonatomic, strong) BottomTextFieldView *btView;

@property (nonatomic, strong) BottomCollectionView *bcView;

@property (nonatomic, strong) HoldingModel *model;

@property (nonatomic, strong) RollingView *rollingView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self configUI];
}

#pragma mark 初始化UI控件
- (void)initUI{
    self.btView = [BottomTextFieldView new];
    self.model = [self loadModel];
    __weak typeof(self) weakSelf = self;
    self.btView.block = ^(NSString *text) {
        weakSelf.model.content = text;
        weakSelf.model = weakSelf.model;
    };
    [self.btView.setttingButton addTarget:self action:@selector(settingClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btView];
    self.view.backgroundColor = [UIColor blackColor];
    self.bcView = [BottomCollectionView new];
    self.bcView.hidden = YES;
    self.bcView.model = self.model;
    self.bcView.block = ^(HoldingModel *model) {
        if (model == nil) {
            weakSelf.btView.hidden = NO;
        }else{
            weakSelf.model = model;
        }
    };
    [self.view addSubview:self.bcView];
}

- (void)setModel:(HoldingModel *)model{
    _model = model;
    if (model) {
        self.rollingView.model = model;
    }
}

#pragma mark 为UI控件添加约束
- (void)configUI{
    [self.btView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_offset(80);
    }];
    [self.bcView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_offset(0);
    }];
}

- (void)settingClick:(UIButton *)sender{
    self.btView.hidden = YES;
    self.bcView.hidden = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.5 animations:^{
        if (self.btView.alpha == 0 && self.bcView.hidden) {
            self.btView.alpha = 1;
        }else if (self.btView.alpha == 1) {
            self.btView.alpha = 0;
        }
    } completion:^(BOOL finished) {
        
    }];
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}

#pragma mark 初始化Model 设置默认值
- (HoldingModel *)loadModel{
    HoldingModel *model = [HoldingModel new];
    model.content = @"请输入的文字弹幕会全部在这里滚动出现";
    model.speed = @"0.5";
    model.fontSize = @"48";
    model.fontName = @"Thonburi";
    model.color = [UIColor whiteColor];
    model.speedArray = @[@"0",@"0.5",@"1",@"1.5"];
    model.fontNameArray = @[@"Copperplate-Light",@"AppleSDGothicNeo-Thin",@"Thonburi",@"GillSans-Italic",@"MarkerFelt-Thin",@"HiraMaruProN-W4"];
    model.colorArray = @[[UIColor whiteColor],[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor]];
    model.fontSizeArray = @[@"24",@"36",@"48",@"64",@"72",@"84"];
    return model;
}

- (RollingView *)rollingView{
    if (!_rollingView) {
        _rollingView = [RollingView new];
        [self.view addSubview:_rollingView];
        [self.view sendSubviewToBack:_rollingView];
        //        CGSize size = CGSizeMake(self.view.bounds.size.height, self.view.bounds.size.width);
        CGSize size = CGSizeMake(self.view.bounds.size.height, self.view.bounds.size.width);
        CGRect rect;
        rect.size = size;
        rect.origin = CGPointMake(0, 0);
        _rollingView.frame = rect;
        _rollingView.center = self.view.center;

        _rollingView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    return _rollingView;
}

@end
